var app = getApp();
Page({
  data: {
    showLoading: true,
    noMoretip: false,
    page: 0,
    curShow: 'good'
  },
  onLoad: function (e) {
    var that = this;
    var type = that.data.curShow;
    if (type == 'good') {
      that.requestOrder();
    } else if (type == 'coupon') {
      that.requestCouponlist();
    }
  },
  onShow: function () {
    app.setNavtitle('兑换详情');
    var that = this;
  },
  toggleShow:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      showLoading: true,
      noMoretip: false,
      page: 0,
      orderList:null,
      curShow: type
    })
    if(type == 'good'){
      that.requestOrder();
    }else if(type == 'coupon'){
      that.requestCouponlist();
    }
  },
  requestOrder: function () {
    var that = this;
    var page = that.data.page;
    var data = {
      map: 'applet_community_order_list',
      page: page,
      type: 'points'
    };
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(page);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.orderList;
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            orderList: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.orderList);
        } else {
          console.log(page);
          if (page <= 0) {
            that.setData({
              orderList: [],
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(res.data);
        }
      },
      complete: function () {
        wx.stopPullDownRefresh();
      }
    });
  },
  requestCouponlist: function () {
    var that = this;
    var page = that.data.page;
    var data = {
      map: 'applet_community_change_list',
      page: page
    };
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(page);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.orderList;
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            orderList: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.orderList);
        } else {
          console.log(page);
          if (page <= 0) {
            that.setData({
              orderList: [],
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(res.data);
        }
      },
      complete: function () {
        wx.stopPullDownRefresh();
      }
    });
  },
  couponGood: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/allgoodsPage/allgoodsPage',
    })
    // if (type == 1) {
    //   wx.navigateTo({
    //     url: '/pages/allgoodsPage/allgoodsPage',
    //   })
    // } else if (type == 2) {
    //   wx.navigateTo({
    //     url: '/subpages/couponGood/couponGood?title=适用商品&id=' + id,
    //   })
    // }
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    var type = that.data.curShow;
    if (type == 'good') {
      that.requestOrder();
    } else if (type == 'coupon') {
      that.requestCouponlist();
    }
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      var type = that.data.curShow;
      if (type == 'good') {
        that.requestOrder();
      } else if (type == 'coupon') {
        that.requestCouponlist();
      }
    }
  },
  todhOrderDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages/jforderDetail/jforderDetail?tid=' + id
    })
  },
})